package com.hn658.base.message.account.dao.redis.impl;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.BoundHashOperations;

import com.hn658.base.message.account.dao.redis.IAccountManagerRedisDAO;
import com.hn658.base.message.account.entity.AccountUserInfoEO;
import com.hn658.base.message.common.utils.RedisKeyHelper;
import com.hn658.framework.dataaccess.redis.AbstractRedisDAO;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class AccountManagerRedisDAOImpl extends AbstractRedisDAO<AccountUserInfoEO> implements IAccountManagerRedisDAO{


    @Autowired
    @Qualifier("redisTemplate")
    @Override
    public void setTemplate(StringRedisTemplate template) {
        super.template = template;
    };

	@Override
	public void save(AccountUserInfoEO accountInfo) {
		if (accountInfo != null && accountInfo.getAppKey() != null&&accountInfo.getUserId()!=null) {
			// 从redis中获取对应appUid的UserInfo
			BoundHashOperations<String, String, String> userOps = template
	                .boundHashOps(RedisKeyHelper.accountInfo(accountInfo.getAppKey(), accountInfo.getUserId()));
			// 保存所有用户信息
			userOps.put("id", String.valueOf(accountInfo.getId()));
			userOps.put("appKey", String.valueOf(accountInfo.getAppKey()));
			userOps.put("userId",accountInfo.getUserId());
			userOps.put("nickName", accountInfo.getNickName());
			if(accountInfo.getAvatarURL()!=null){
				userOps.put("avatarURL", accountInfo.getAvatarURL());
			}
			userOps.put("isDeleted", String.valueOf(accountInfo.getIsDeleted().toString()));
			if (accountInfo.getCreateTime() != null) {
				userOps.put("createTime",String.valueOf(accountInfo.getCreateTime().getTime()));
			}
			if (accountInfo.getLastUpdateTime() != null) {
				userOps.put("lastUpdateTime",String.valueOf(accountInfo.getLastUpdateTime().getTime()));
			}
		}
		
	}

	@Override
	public AccountUserInfoEO getAccount(String appKey, String userId) {
		AccountUserInfoEO accountUserInfoEO=null;
		if(StringUtils.isNotEmpty(appKey) && userId!=null){
			BoundHashOperations<String, String, String> userOps = template.boundHashOps(RedisKeyHelper.accountInfo(appKey, userId));
			Map<String, String> entries = userOps.entries();
			entries.remove(RedisKeyHelper.lastActiveDateTime());
			if(entries!=null&&!entries.isEmpty()) {
				accountUserInfoEO = mapper.fromHash(entries);
			}
		}
		return accountUserInfoEO;
	}

	@Override
	public void delete(String appKey, String userId) {
		String accessToken = valueOps.get(RedisKeyHelper.tokenByAppUid(appKey, userId));
		template.delete(RedisKeyHelper.tokenByAppUid(appKey, userId));
		template.delete(RedisKeyHelper.appUidByToken(accessToken));
		template.delete(RedisKeyHelper.accountInfo(appKey, userId));
	}

	@Override
	public AccountUserInfoEO getAccountByAccessToken(String accessToken) {
		String appUid=valueOps.get(RedisKeyHelper.appUidByToken(accessToken));
		String[] strs = RedisKeyHelper.unMarshal(appUid);
		return getAccount(strs[0], strs[1]);
	}

	@Override
	public void setAccessToken(String appKey, String userId, String accessToken) {
		String authToken = valueOps.get(RedisKeyHelper.tokenByAppUid(appKey, userId));
		// 删除之前的相互关系
		if (StringUtils.isNotEmpty(authToken)) {
			template.delete(RedisKeyHelper.tokenByAppUid(appKey, userId));
			template.delete(RedisKeyHelper.appUidByToken(authToken));
		}
		// 创建token
		valueOps.set(RedisKeyHelper.tokenByAppUid(appKey, userId), accessToken);
		valueOps.set(RedisKeyHelper.appUidByToken(accessToken), RedisKeyHelper.marshal(appKey, userId));
	}

	@Override
	public String getAccessToken(String appKey, String userId) {
		return valueOps.get(RedisKeyHelper.tokenByAppUid(appKey, userId));
	}

	@Override
	public void setUserOnlineStatus(String appKey, String userId, boolean isOnline) {
        BoundHashOperations<String, String, String> userOps = template
                .boundHashOps(RedisKeyHelper.accountInfo(appKey, userId));
        if (isOnline) {
        	userOps.put(RedisKeyHelper.lastActiveDateTime(), String.valueOf(System.currentTimeMillis()));
        } else {
        	userOps.put(RedisKeyHelper.lastActiveDateTime(), "0");
        }
	}

}
